
/**
 * user vuex
 */
import Vue from 'vue'
import posts from '@/plugins/request'
import storage from '@/plugins/localStorage'
// initial state
const state = {
  userInfo: {},
  stationList: [],
  vehicleNumList: [],
  curPageCanDo: [],
  userAdminList: {
    data: [],
    total: 0,
  },
  warningWay: null
}
// getters
const getters = {
  getStationList: state => {
    if(state.stationList.length) {
      return state.stationList
    }else if(storage.get('myStation')) {
      return JSON.parse(storage.get('myStation'))
    }else {
      return []
    }
  },
  getVehicleNumList: state => {
    if(state.vehicleNumList.length) {
      return state.vehicleNumList
    }else if(storage.get('vehicleList')) {
      return JSON.parse(storage.get('vehicleList'))
    }else {
      return []
    }
  },
  getUserInfo: state => {//??????
    if(JSON.stringify(state.userInfo) != '{}') {
      return state.userInfo
    }else if(storage.get('userInfo')) {
      return JSON.parse(storage.get('userInfo'))
    }else {
      return []
    }
  },
  getCurPageCanDo: state => state.curPageCanDo,
  getUserAdminList: state => state.userAdminList,
  getWarningWay: () => JSON.parse(storage.get('userInfo')).user.pcinforme,
}
// actions
const actions = {
  async login ({ commit }, params) {
    let account = params.account
    let password = params.password
    let result = await posts.login(account, password)
    return result
  },
  async getStationListName( { commit },param) {
    let result = await posts.fetchStationNameList(param)
    commit('changeStationList', result)
  },
  async getVehicleNumList({ commit }) {
    let result = await posts.fetchVehicleNum()
    commit('changeVehicleNumList', result)
  },
  async fetchUserList({ commit }, param) {
    let data = await posts.fetchUserList(param)
    commit('changeUseAdminList', data)
  },
  async postSystemUserAdd({ commit }, formdata) {
    let result = await posts.postSystemUserAdd(formdata)
    return result
  },
  async fetchUserDetail({ commit }, account) {
    let data = await posts.fetchUserDetail(account)
    return data
  },
  async putUpdatePassword({ commit }, params) {
    let result = await posts.putUpdatePassword(params)
    return result
  },
  async putForgetPassword({ commit }, params) {
    let result = await posts.putForgetPassword(params)
    return result
  },
  async postUserRegister({ commit }, params) {
    let result = await posts.postUserRegister(params)
    return result
  },
  async doLoginOut({ commit }, id) {
    let result = await posts.loginOut(id)
    return result
  },

}

// mutations
const mutations = {
  updateUserToken(state, token) {
    storage.set('token', token)
  },
  updateUserInfo(state, obj) {
    //userInfo和token 存到vuex 和 storage
    state.userInfo = obj
    storage.set('userInfo', obj)
  },
  changeStationList(state, data) {
    storage.set('myStation', data)
    Vue.set(state,'stationList', data)
  },
  changeVehicleNumList(state, data) {
    storage.set('vehicleList', data)
    state.vehicleNumList = data
  },
  changeCurPageCanDo(state, cando) {
    state.curPageCanDo = cando
  },
  changeUseAdminList(state, data) {
    state.userAdminList.data = data.rows
    state.userAdminList.total = data.total
  },
  changeWarningWay(state, flag) {
    state.warningWay = flag
  }
}
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
